<template>
  <div>
    <el-form label-width="100px" :rules="rules" :model="form" ref="formRef">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select placeholder="请选择" v-model="form.manager">
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="hCancel">取消</el-button>
        <el-button type="primary" @click="Hsubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 调用api
import {
  getEmployeeSimple,
  addDepartments,
  getDepartDetail,
  updateDepartments,
} from "@/api/departments";

export default {
  name: "VueTwoDeptDialog",
  // 父传子
  props: {
    pid: {
      // 类型
      type: String,
      // 必传
      required: true,
    },
    isEdit: {
      // 类型
      type: Boolean,
      // 必传
      required: true,
    },
    originList: {
      // 类型
      type: Array,
      // 必传
      required: true,
    },
  },
  data() {
    const validCode = (ruls, value, callback) => {
      let codeList = this.originList.map((item) => item.code);
      console.log(codeList);
      if (this.isEdit) {
        codeList = this.originList
          .filter((item) => item.id !== this.pid)
          .map((item) => item.code);
      }
      if (codeList.includes(value)) {
        callback(new Error(value + "被占用了"));
      } else {
        callback();
      }
    };
    const validName = (ruls, value, callback) => {
      // 当前被点击的部门id 为 this.pid
      let nameList = this.originList
        .filter((item) => item.pid === this.pid)
        .map((item = item.name));
        if(this.isEdit){
          // 如果是编辑机状态 不能取的名字:当前部门的兄弟部门 - 自己的名字
          const item =  this.originList.filter(item => item.id === this.pid)
          const pid = item.pid
          nameList = this.originList.filter(item => item.pid === this.pid && item.id !=this.pid).map(item => item.name)
        }
      if (nameList.includes(value)) {
        callback(new Error(value + "被占用了"));
      } else {
        callback();
      }
    };

    return {
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门名称要求1-50个字符",
            trigger: "blur",
          },
          { validator: validName, trigger: "blur" },
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门编码要求1-50个字符",
            trigger: "blur",
          },
          { validator: validCode, trigger: "blur" },
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            min: 1,
            max: 300,
            message: "部门介绍要求1-300个字符",
            trigger: "blur",
          },
        ],
      },
      // 双向绑定
      form: {
        code: "",
        introduce: "",
        manager: "",
        name: "",
      },
      // 员工列表 v-for
      list: [],
    };
  },
  // 第一次加载自动发送ajax
  created() {
    // 获取员工简单列表方法
    this.loadSimpleEmployee();
    // 编辑部门
    this.loadDetail();
  },
  methods: {
    // 调用 api
    async loadSimpleEmployee() {
      const res = await getEmployeeSimple();
      console.log("获取获取简单员工列表", res);
      this.list = res.data;
      console.log(this.list);
    },
    async loadDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.pid);
        console.log("编辑", res);
        // 数据回填
        this.form = res.data;
      }
    },
    // 确认操作
    async dAdd() {
      try {
        // 确认按钮 发送api
        const res = await addDepartments({ ...this.form, pid: this.pid }); //接口文档需要的数据
        // 子传父
        this.$emit("success");
      } catch (err) {
        console.log(err);
      }
    },
    // 编辑操作
    async dEdit() {
      try {
        await updateDepartments(this.form);
        this.$emit("success");
      } catch (err) {
        console.log(err);
      }
    },
    Hsubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.isEdit ? this.dEdit() : this.dAdd();
        }
      });
      // this.isEdit ? this.dEdit() :this.dAdd()
    },
    // 取消按钮
    hCancel() {
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>